<?php

class Chrono {

	private $timeStart;
	private $timeEnd;
	
	public function __construct() {
	}

	public function start() {
		$time = explode(' ',microtime() ); 
		$this->timeStart = $time[1].substr($time[0], 1);
	}

	public function stop($precision = 4) {
		$time = explode(' ',microtime() );
		$this->timeEnd = $time[1].substr($time[0], 1);

		$timeElapse = number_format($this->timeEnd - $this->timeStart, 4); 
		if ($precision > strlen($timeElapse)) {
			//si la precision demandée est plus grande que la longueur de la chaine
			//on donne la precision maximale
			$timeElapse = substr($timeElapse, 0, strlen($timeElapse));
		} else {
			$timeElapse = substr($timeElapse, 0, $precision);
		}
		return $timeElapse; 
	}

	public function affiche($precision = 4) {
		echo "Temps d'exécution : ".$this->stop($precision)." seconde(s)<br>";
		
	}
}
?>